package com.yuan.securitydemo.controller;

import com.yuan.securitydemo.pojo.User;
import com.yuan.securitydemo.securtiy.UserDetailsServiceImpl;
import com.yuan.securitydemo.securtiy.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@Controller
public class UserController {
    @Autowired
    private UserDetailsServiceImpl userDetailsService;

    @PostMapping("/doRegister")
    public String register(User user) {
        log.debug(user.toString());

        boolean register = userDetailsService.register(user);
        return "login";
    }

    @GetMapping("/getCurrentUser")
    @ResponseBody
    public String test(){
        User principle = SecurityUtils.getCurrentUser();
        return principle.toString();
    }


}
